 /*
 * @description: 新增编辑页(选择商品)
 * @Author: along
 * @Date: 2020-11-14
 * @Last Modified by: along
 * @Last Modified time: 2020-11-17
 */
<template>
    <div
        class="couponChangeShop"
        v-if="dialogVisible"
    >
        <el-dialog
            :title="TYPE ? '选择可用商品' : '选择不可用商品'"
            :visible.sync="dialogVisible"
            width="1200px"
            :close-on-click-modal="false"
        >
            <div class="wrap">
                <div class="filter-view">
                    <div class="filter-item">
                        <div class="filter-item__label">商品编号</div>
                        <el-input
                            placeholder="请输入商品编号"
                            v-model="formData.goods_nos"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">商品名称</div>
                        <el-input
                            placeholder="请输入商品名称"
                            v-model="formData.keyword"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">店铺名称</div>
                        <el-input
                            placeholder="请输入店铺名称"
                            v-model="formData.shop_title"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">公司名称</div>
                        <el-input
                            placeholder="请输入公司名称"
                            v-model="formData.company_name"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">商品类型</div>
                        <el-select
                            size="medium"
                            v-model="formData.sub_type_no"
                            placeholder="商品类型"
                            clearable
                            style="width: 188px"
                            @change="getTableData(1)"
                        >
                            <el-option
                                v-for="item in typeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </div>
                    <div class="filter-item">
                        <el-checkbox
                            v-model="formData.status"
                            @change="getTableData(1)"
                        >只显示已上架
                        </el-checkbox>
                    </div>
                    <div class="filter-item">
                        <el-button
                            type="text"
                            size="medium"
                            @click="goShopList()"
                        >
                            商品管理
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="refresh()"
                        >
                            刷新
                        </el-button>
                    </div>
                </div>
                <!--<div class="header">
                    <div class="header-item">
                        <p
                            class="header_system"
                            @click="goShopList()"
                        >
                            商品管理
                        </p>
                        <p
                            class="header_refresh"
                            @click="refresh()"
                        >
                            刷新
                        </p>
                    </div>
                    <div class="header-item">
                        <el-checkbox
                            v-model="formData.status"
                            @change="getTableData(1)"
                        >只显示已上架</el-checkbox>
                        <div class="filter">
                            <div class="filter_type">
                                &lt;!&ndash; <el-cascader
                                    placeholder="请选择"
                                    :options="typeAllList"
                                    filterable
                                    clearable
                                    change-on-select
                                    expand-trigger="click"
                                    v-model="formData.sub_type_no"
                                    @change="getTableData(1)"
                                    :props="{
                                        label: 'title',
                                        value: 'no',
                                        children: 'children'
                                    }"
                                    style="131px"
                                /> &ndash;&gt;
                                <el-select
                                    v-model="formData.sub_type_no"
                                    clearable
                                    filterable
                                    placeholder="请选择"
                                    size="small"
                                    @change="getTableData(1)"
                                >
                                    <el-option
                                        v-for="item in typeList"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </div>
                            <div class="filter_line" />
                            <div class="filter_keyword">
                                <el-input
                                    v-model="formData.keyword"
                                    placeholder="商品编号、商品名称、店铺名称"
                                    size="medium"
                                    clearable
                                    suffix-icon="el-icon-search"
                                    :style="{width: '240px'}"
                                    @change="getTableData(1)"
                                    class="placeStyle"
                                />
                            </div>
                        </div>
                    </div>
                </div>-->

                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    border
                    ref="couponChangeShop"
                    @select="fnSelect"
                    @select-all="fnSelectAllTop"
                    @selection-change="handleSelectionChange"
                    class="tableDate"
                    v-loading="loading"
                    :row-key="getRowKeys"
                >
                    <el-table-column
                        prop="goods_no"
                        type="selection"
                        :select="true"
                        :reserve-selection="true"
                        width="40"
                    />
                    <el-table-column
                        prop="goods_cover"
                        label="商品信息"
                        :formatter="emptyFormatter"
                        align="left"
                        width="200"
                    >
                        <template slot-scope="scope">
                            <div class="info">
                                <img
                                    :src="scope.row.goods_cover"
                                    :style="{width: '64px', height: '64px', marginRight: '12px'}"
                                    @click="fnShowPreview(scope.row.goods_cover)"
                                >
                                <el-tooltip
                                    effect="dark"
                                    :content="scope.row.goods_title"
                                    placement="top">
                                    <div class="info_main">
                                        {{ scope.row.goods_title }}
                                    </div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_no"
                        label="商品编号"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="shop_title"
                        label="店铺名称"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_type"
                        label="商品类型"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <p v-if="scope.row.goods_type == 1">自营</p>
                            <p v-else-if="scope.row.goods_type == 3">秒杀</p>
                            <p v-else-if="scope.row.goods_type == 5">折扣专区</p>
                            <!-- <p v-else-if="scope.row.goods_type == 7">砍价</p> -->
                            <p v-else>--</p>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_price"
                        label="学生价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            {{ `￥${scope.row.goods_price}` }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_price"
                        label="会员价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            {{ `￥${scope.row.goods_price}` }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_stock"
                        label="库存"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="goods_sale"
                        label="真实销量"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="其他活动优惠"
                        :formatter="emptyFormatter"
                        align="center"
                        width="180"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.benefit_list && scope.row.benefit_list.length">
                                <div
                                    v-for="(item,index) in scope.row.benefit_list"
                                    :key="index"
                                >
                                    {{ item.info }}
                                </div>
                            </div>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- /分页 -->
                <div class="pagination-wrapper">
                    <el-checkbox
                        v-model="checkedAll"
                        :indeterminate="checkIndeterminate"
                        @change="fnChangeChecked"
                    >全选</el-checkbox>
                    <el-pagination
                        background
                        layout="total, prev, pager, next, jumper"
                        :current-page.sync="page.now_page"
                        :total="page.total_count"
                        :page-size="page.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="medium"
                    :style="{width: '126px', marginRight: '52px'}"
                >
                    取 消
                </el-button>
                <el-button
                    type="primary"
                    :style="{width: '126px'}"
                    @click="confirm()"
                >
                    确 定
                </el-button>
            </span>
        </el-dialog>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
export default {
    name: 'CouponChangeShop',
    components: {
        imgPreview
    },
    data () {
        return {
            loading: false,
            dialogVisible: false,
            TYPE: '',//true: 选择可用商品 false: 选择不可用商品
            tableData: [],//表格数据
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            // changeData: {},//选择数据
            handleData: [],//修改数据，进行回填数据
            typeAllList: [],//商品分类数据列表
            formData: {
                status: true,//上架状态
                sub_type_no: '',//商品类型
                keyword: '',//商品名称或编号
                goods_nos: '',
                shop_title: '',
                company_name: ''
            },
            checkIndeterminate: false,
            multipleSelection: [],
            checkedAll: false,
            // typeList: [{value: 1, label: '自营'},{value: 3, label: '秒杀'},{value: 5, label: '折扣专区'},{value: 7, label: '砍价'}]
            typeList: [{value: '', label: '全部'},{value: 1, label: '自营'},{value: 5, label: '折扣专区'},{value: 3, label: '秒杀'}]
        };
    },
    watch: {
        dialogVisible (val) {
            if(!val) {
                this.checkedAll = this.checkIndeterminate = false;
                this.multipleSelection = [];
            }
        }
    },
    mounted () {
        // this.getTypeList(); 废弃
    },
    methods: {
        /**
         * @description 显示弹窗
         *      type: true: 选择可用商品 false: 选择不可用商品
         *      next:  true: 回显
         */
        show (type, next) {
            this.dialogVisible = true;
            this.TYPE = type;

            if(!next) {
                this.getTableData(1);
            }
        },

        /**
         * @description 关闭弹窗
         */
        close () {
            this.dialogVisible = false;
        },

        /**
         * @description 获取商品数据
         */
        getTableData (page) {
            this.checkedAll = false;
            this.checkIndeterminate = false;
            this.$post('/student/shop_goods%5Cget_list_for_select', {
                page_id: page || this.page.now_page,
                goods_type: this.formData.sub_type_no,
                only_sale: this.formData.status ? 1 : 0,
                keyword: this.formData.keyword,
                goods_nos: this.formData.goods_nos,
                shop_title: this.formData.shop_title,
                company_name: this.formData.company_name,
            }, resp => {
                if (resp.code == 1) {
                    if(Object.prototype.toString.call(resp.data) == '[object Null]') return false;
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 获取商品分类数据
         */
        // getTypeList () {
        //     this.$post('/student/shop_goods_type%5Cget_list_linkage', {}, resp => {
        //         if(resp.code == 1){
        //             this.typeAllList = resp.data.list;
        //         }
        //     });
        // },

        /**
         * @description 修改数据回填
         */
        setData (list) {
            // this.tableData = JSON.parse(JSON.stringify(list));
            // this.page.now_page = 1;
            // this.page.total_count = this.tableData.length;
            // this.page.page_size = this.tableData.length;
            // this.checkedAll = true;
            // this.checkIndeterminate = false;
            this.toggleRowSelect(JSON.parse(JSON.stringify(list)));
            this.refresh();
        },

        /**
         * @description 表格数据回显条件
         */
        getRowKeys (row) {
            return row.goods_no;
        },
        toggleRowSelect(rows) {
            const _this = this;

            if (rows.length > 0) {
                this.$nextTick(() => {
                    rows.forEach(row => {
                        _this.$refs.couponChangeShop.toggleRowSelection(row, true);
                    });
                });
            } else {
                this.$refs.couponChangeShop.clearSelection();
            }
        },

        /**
         * @description 确定选择商品
         */
        confirm () {
            // console.log('multipleSelection', this.multipleSelection);
            if(!this.multipleSelection.length) {
                this.$notify({
                    title: '提示',
                    message: '请先选择商品',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            const nos = this.multipleSelection.reduce((cur,next) => {
                cur.push(next.goods_no);
                return cur;
            },[]);

            const person = data => {
                let obj = {};
                let list = data;

                list = list.reduce((cur,next) => {
                    // eslint-disable-next-line no-unused-expressions
                    obj[next.goods_no] ? "" : obj[next.goods_no] = true && cur.push(next);
                    return cur;
                },[]);

                return list;
            };

            this.$emit('change', {
                list: person(this.multipleSelection),
                type: this.TYPE
            });
            this.close();
        },

        /**
         * @description 刷新
         */
        refresh () {
            this.loading = true;
            this.checkedAll = false;
            this.getTableData();
            setTimeout(() => {
                this.loading = false;
            },300);
        },

        /**
         * @description 全选逻辑操作
         */
        //单选
        fnSelect(selection) {
            if(selection.length == this.tableData.length) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        //全选
        fnSelectAllTop(selection) {
            if(selection.length > 0) {
                this.checkedAll = true;
            }else{
                this.checkedAll = false;
            }
            if(selection.length > 0 && selection.length < this.tableData.length) {
                this.checkIndeterminate = true;
            }else{
                this.checkIndeterminate = false;
            }
        },
        //全选
        fnChangeChecked() {
            if(this.checkedAll){
                this.toggleSelection(1);
            }else{
                this.toggleSelection();
            }
        },
        toggleSelection(rows) {
            if (rows) {
                this.$refs.couponChangeShop.toggleAllSelection();
            } else {
                this.$refs.couponChangeShop.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 商品管理
         */
        goShopList () {
            let routeData = this.$router.resolve({
                name: 'GoodsList'
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.couponChangeShop {
    .dialog-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 10px;
    }
    .wrap {
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        .filter-view {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .filter-item {
                margin-right: 24px;
                margin-bottom: 24px;
                display: flex;
                align-items: center;
                &__label {
                    margin-right: 12px;
                }
            }
        }
        .header {
            height: 36px;
            display: flex;
            align-items: center;
            box-sizing: border-box;
            margin-bottom: 24px;
            justify-content: space-between;
            .header_system {
                color: rgba(24, 144, 255, 1);
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                white-space: nowrap;
            }
            .header_refresh {
                color: rgba(24, 144, 255, 1);
                font-size: 14px;
                font-weight: 500;
                cursor: pointer;
                margin-left: 32px;
                margin-right: 122px;
                white-space: nowrap;
            }
            .filter {
                width: 372px;
                height: 36px;
                overflow: hidden;
                box-sizing: border-box;
                border-radius: 4px;
                border: 1px #c2c2c2 solid;
                display: flex;
                align-items: center;
                margin-left: 16px;
                .filter_type {
                    flex: 1;
                }
                .filter_line {
                    width: 0px;
                    height: 14px;
                    border-left: 1px #999 solid;
                }
            }
            .header-item {
                display: flex;
                align-items: center;
            }
        }
        .tableDate {
            .info {
                display: flex;
                align-items: center;
            }
            .info_main {
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }
        }
        /* 分页 */
        .pagination-wrapper {
            background-color: #fbfbfb;
            box-sizing: border-box;
            border: 1px #EBEEF5 solid;
            border-top: 0;
            border-radius: 0 0 4px 4px;
            padding: 14px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }

    /deep/ .el-dialog__body {
        padding: 24px 32px;
    }
    /deep/ .el-dialog__footer {
        padding: 0 0 32px;
    }
    /deep/ .el-dialog {
        margin-top: 7vh!important;
    }
}
</style>
<style lang="less">
.couponChangeShop {
    .el-dialog__body {
        padding: 24px 32px;
    }
    .filter_keyword {
        .el-input__inner {
            border: none;
        }
    }
    .filter_type {
        .el-input__inner {
            border: none;
        }
    }
    .el-table__body-wrapper {
        height: auto !important;
        max-height: 350px;
    }
    .el-table__row {
        min-height: 85px;
        height: 85px;
    }
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
    .el-table th, .el-table tr {
        background-color: #fbfbfb;
    }
    .placeStyle {
        &::-webkit-input-placeholder {
            font-size: 12px;
        }
    }
}
</style>
